﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data.SqlServerCe;

/// <summary>
/// Summary description for DataAccess
/// </summary>
public class DataAccess
{
    //Database Variables
    private  SqlCeConnection Connection;
    private SqlCeCommand Command;
    private SqlCeDataReader reader;

    /// <summary>
    /// Get or Set the CommandString to execute
    /// </summary>
    public string CommandString
    {
        get
        {
            return Command.CommandText;
        }
        set
        {
            Command.CommandText = value;
        }
    }

    /// <summary>
    /// Returns the connection state of the SqlCeDataAccess2003 object
    /// </summary>
    public System.Data.ConnectionState State
    {
        get
        {
            return Connection.State;
        }
    }

    /// <summary>
    /// Add SqlCeParameter
    /// </summary>
    /// <param name="ParamName"></param>
    /// <param name="value"></param>
    public void AddParameter(string ParamName, object value)
    {
        Command.Parameters.Add(new SqlCeParameter(ParamName, value));
    }

    /// <summary>
    /// Change the value of the parameter by it's name
    /// </summary>
    /// <param name="ParamName"></param>
    /// <param name="value"></param>
    public void SetParameter(string ParamName, object value)
    {
        Command.Parameters[ParamName].Value = value;
    }

    /// <summary>
    /// Remove a parameter by it's name
    /// </summary>
    /// <param name="ParamName"></param>
    public void RemoveParameter(string ParamName)
    {
        Command.Parameters.Remove(Command.Parameters[ParamName]);
    }

    /// <summary>
    /// Get Parameter value by it's name
    /// </summary>
    /// <param name="ParamName"></param>
    /// <returns></returns>
    public object GetParameter(string ParamName)
    {
        return Command.Parameters[ParamName].Value;
    }

    /// <summary>
    /// Clear all command Parameters
    /// </summary>
    public void ClearParameters()
    {
        Command.Parameters.Clear();
    }

    /// <summary>
    /// Get the SqlCeDataReader
    /// </summary>
    public SqlCeDataReader Reader
    {
        get
        {
            return reader;
        }
    }

    /// <summary>
    /// Execute the Command and make the reader ready
    /// </summary>
    public void ExecuteReader()
    {
        if (reader != null)
        {
            if (!reader.IsClosed)
            {
                reader.Close();
            }
        }
        if (Connection.State != System.Data.ConnectionState.Open) Connection.Open();
        reader = Command.ExecuteReader();
    }

    /// <summary>
    /// Execute the command in NonQuery mode
    /// </summary>
    public void ExecuteNonQuery()
    {
        if (Connection.State != System.Data.ConnectionState.Open) Connection.Open();
        Command.ExecuteNonQuery();
    }

    /// <summary>
    /// Close the connection
    /// </summary>
    public void Close()
    {
        if (Connection.State != System.Data.ConnectionState.Closed) Connection.Close();
    }

    /// <summary>
    /// A call has been made for SqlCeDataAccess
    /// </summary>
    public DataAccess(string DatabaseName, string Password)
	{
        string ConnectionString;
        //Database file
        string path = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
        if (path[path.Length - 1] == '\\') path = path.Substring(0, path.Length - 1);
        string f_name = path + "\\App_Data\\" + DatabaseName + ".sdf";
        ConnectionString = "Data Source=" + f_name + ";Encrypt Database=True;Password=" + Password + ";File Mode=shared read;Persist Security Info=False;";
        try
        {
            SqlCeEngine engine = new SqlCeEngine(ConnectionString); engine.Upgrade();
        }
        catch { }
        Connection = new SqlCeConnection(ConnectionString);
        Command = new SqlCeCommand("", Connection);

	}

    /// <summary>
    /// Dispose the object
    /// </summary>
    ~DataAccess()
    {
    //    if (Connection.State != System.Data.ConnectionState.Closed) Connection.Close();
    }

}